﻿// Copyright @ shch8.com All Rights Reserved At 2009-3-23
//开发：商创技术（www.shch8.com）望月狼
/*
·声音加载与简单控制，同一项目中需要加载多个音乐请不要使用单例，改用New

 例：
 import com._public._net.LoadSound
 import flash.utils.Timer;
 LoadSound.getInstance().loadSound("music.mp3",2)
 */
package com._public._net {
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.TimerEvent;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.media.SoundLoaderContext;
	import flash.media.SoundTransform;
	import flash.net.URLRequest;
	import flash.utils.Timer;
	public class LoadSound {
		private var _sound:Sound=new Sound;
		private var music:SoundChannel = new SoundChannel;
		private static var Instance:LoadSound = new LoadSound;
		private var pos:Number = 0;
		private var timer:Timer;
		/**
		 *循环次数 
		 */		
		public var cycle:Number;
		/**
		 * 声音控制增量,停止与开始用的渐隐值
		 */		
		public var conNum:Number=10;
		private var volueNum:Number;//当前音量
		private var volueNum_c:Number;//音量备份
		private var soundEffect:Boolean;
		/**
		 * 加载失败回调方法
		 * */
		public var callLoadErr:Function;
		public static function getInstance():LoadSound {
			return Instance;
		}
		/**
		 * 加载mp3文件(路径,缓冲秒数默认1秒-单位“秒”,播放次数-1为循环播放,使用渐渐效果)
		 * @param _filePath
		 * @param context
		 * @param _cycle
		 * @param _soundEffect
		 * 
		 */		
		public function loadSound(_filePath:String, context:Number = 1, _cycle:Number = -1,_soundEffect:Boolean=true):void {
			cycle = _cycle;
			soundEffect=_soundEffect;
			_sound=new Sound;
			var con:SoundLoaderContext = new SoundLoaderContext(context*1000,false);
			_sound.load(new URLRequest(_filePath),con);
			_sound.addEventListener(IOErrorEvent.IO_ERROR, loadError);
			timer = new Timer(200)
			timer.addEventListener(TimerEvent.TIMER, enterFrame);
			play();
			
		}
		/**
		 *停止 
		 * 
		 */		
		public function stop():void {
			pos=0
			useEffect(false);
			}
		/**
		 *播放 
		 */
		public function play():void {
			var soundv:SoundTransform = new SoundTransform();
			music = new SoundChannel;
			music.soundTransform = soundv;
			this.volueNum = 100;
			music = _sound.play(pos);
			music.addEventListener(Event.SOUND_COMPLETE, soundCompleteHandler);
			if(soundEffect){
				soundv.volume = 0;
				useEffect(true)
			}
			}
		/**
		 *暂停 
		 * 
		 */		
		public function pause():void {
				useEffect(false)
			}
		/**
		 *音量 
		 * @param Num
		 */		
		public function set volume(Num:Number):void {
			if (Num < 0) {
				Num = 0;
				}
			if (Num > 100) {
				Num = 100;
				}
			volueNum = Num;
			var soundv:SoundTransform = new SoundTransform();
			soundv.volume = Num/100;
			music.soundTransform=soundv;
			}
		private function useEffect(actPlay:Boolean):void {
			if (actPlay) {//打开声音的增量
				conNum = Math.abs(conNum);
				volueNum_c = 0;
				}else {
					conNum = Math.abs(conNum)*-1;
					}
			if(timer!=null)timer.start();
			}
		private function enterFrame(event:TimerEvent):void {
			if (volueNum_c > volueNum) {
				volueNum_c = volueNum;
				}
			var soundv:SoundTransform = new SoundTransform();
			soundv.volume = volueNum_c/100;
			music.soundTransform = soundv;
			volueNum_c += conNum;
			if (volueNum_c > volueNum) {//开启声音
				timer.stop();
				}else if (volueNum_c < 0) {//关闭声音
					if (pos != 0) {//暂停 否则为停止
						pos = music.position;
						}
					music.stop();
					timer.stop();
					}
			}
		private function soundCompleteHandler(event:Event):void {
			pos = 0;
			if (cycle == -1) {//声音循环
				play()
				return;
				}
				if (cycle > 1) {
					cycle--
					play()
					}
		}
		private function loadError(event:IOErrorEvent):void {
					this.callLoadErr();
				}
	}
}